//
//  UIView+UIView_Grid.m
//  yilutongxing
//
//  Created by T&J on 2017/7/11.
//  Copyright © 2017年 YZE. All rights reserved.
//
#import "UIView+Grid.h"

@implementation UIView (Grid)

//创建grid
+ (UIView*)createGrid:(GridBorder)border backgroundColor:(UIColor*)bgcolor titleColor:(UIColor*)titlecolor titleSize:(NSInteger)titlesize origin:(CGPoint) originPoint size:(CGSize)size{
    UIView *grid = [[UIView alloc]initWithFrame:CGRectMake(originPoint.x, originPoint.y, size.width, size.height)];//外背景
    grid.backgroundColor = COLOR(230, 230, 230);
    UIView *innerGrid = [[UIView alloc]initWithFrame:CGRectMake(0, 0, grid.frame.size.width, grid.frame.size.height)];//内背景，用于绘制边框
    innerGrid.tag = INNERGRID_TAG;
    innerGrid.backgroundColor = bgcolor;
    if ((border & GridBorderLeft)==GridBorderLeft) {
        CGRect frame = innerGrid.frame;
        frame.origin.x = 1;
        frame.size.width = grid.frame.size.width-1;
        innerGrid.frame = frame;
    }
    if ((border & GridBorderTop)==GridBorderTop) {
        CGRect frame = innerGrid.frame;
        frame.origin.y = 1;
        frame.size.height = grid.frame.size.height - 1;
        innerGrid.frame = frame;
    }
    if ((border & GridBorderRight)==GridBorderRight) {
        CGRect frame = innerGrid.frame;
        frame.size.width = grid.frame.size.width - 1 - frame.origin.x;
        innerGrid.frame = frame;
    }
    if ((border & GridBorderBottom)==GridBorderBottom) {
        CGRect frame = innerGrid.frame;
        frame.size.height = grid.frame.size.height - 1 - frame.origin.y;
        innerGrid.frame = frame;
    }
    [grid addSubview:innerGrid];
    
    //表格内容标签
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(3, 3, innerGrid.width - 6, innerGrid.height - 6)];
    titleLabel.numberOfLines = 0;
    titleLabel.textColor = titlecolor;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.tag = INNERLABEL_TAG;
    titleLabel.font = [UIFont systemFontOfSize:titlesize];
    [grid addSubview:titleLabel];
    
    return grid;
}

+ (void)refreshBorder:(GridBorder)border forGrid:(UIView*)grid{
    UIView *innerGrid = [grid viewWithTag:INNERGRID_TAG];
    if ((border & GridBorderLeft)==GridBorderLeft) {
        CGRect frame = innerGrid.frame;
        frame.origin.x = 1;
        frame.size.width = grid.frame.size.width-1;
        innerGrid.frame = frame;
    }
    if ((border & GridBorderTop)==GridBorderTop) {
        CGRect frame = innerGrid.frame;
        frame.origin.y = 1;
        frame.size.height = grid.frame.size.height - 1;
        innerGrid.frame = frame;
    }
    if ((border & GridBorderRight)==GridBorderRight) {
        CGRect frame = innerGrid.frame;
        frame.size.width = grid.frame.size.width - 1 - frame.origin.x;
        innerGrid.frame = frame;
    }
    if ((border & GridBorderBottom)==GridBorderBottom) {
        CGRect frame = innerGrid.frame;
        frame.size.height = grid.frame.size.height - 1 - frame.origin.y;
        innerGrid.frame = frame;
    }
}

@end
